<?php
namespace Application\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

class VideoUploader extends AbstractPlugin
{
	public function __invoke($fieldName='fileupload', $uploadPath="", $sizeParams = array(), $extensions = array('mp3', 'mp4', 'avi', 'flv'))
	{
		if($uploadPath==null||$uploadPath==""){
			$config = $this->controller->getServiceLocator()->get('config');
			$uploadPath = $config['module_config']['videos_location'];
		}
		
		$uploadFile = $this->controller->params()->fromFiles($fieldName);
		//var_dump($uploadFile);
		$adapter = new \Zend\File\Transfer\Adapter\Http();
		$size = new \Zend\Validator\File\Size(array('min' => 1 )); // minimum bytes filesize, max too..
		$extension = new \Zend\Validator\File\Extension(array('extension' => $extensions));
		//$adapter->setValidators(array($size, $extension), $uploadFile['name']);
		
		//echo "here";
		$name = "".time().$uploadFile['name'];
		//var_dump($name);
		//if($adapter->isUploaded($fieldName)){
		if($adapter->isValid()) {
			//$adapter->setDestination($uploadPath);
			$name = "".time().$uploadFile['name'];
			//var_dump($name);
			$adapter->clearFilters();
			$adapter->addFilter('Rename',
					array(
							'target'=> $uploadPath . '/' . $name,
							'overwrite'=>true),
					$fieldName
			);
			if ($adapter->receive($uploadFile['name'], array($fieldName))) {
			    //$adapter->clearFilters();
			    //$adapter->clearValidators();
				return $name;
				//$filename = $adapter->getFileName();
			}
		}
		//}
		return null;
	}
}